-- Autocmds are automatically loaded on the VeryLazy event
-- Default autocmds that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua
--
-- Add any additional autocmds here
-- with `vim.api.nvim_create_autocmd`
--
-- Or remove existing autocmds by their group name (which is prefixed with `lazyvim_` for the defaults)
-- e.g. vim.api.nvim_del_augroup_by_name("lazyvim_wrap_spell")

-- 通用 C/C++ 源文件模板
local config = {
	author = "UnknownCreature1117",    -- 默认作者名
	email = "UnknownCreature1117@outlook.com", -- 联系邮箱
	tool = "Nvim", --使用Nvim
	environment = "Linux & Manjaro",
	license = "MIT",        -- 许可证类型
	use_cpp20 = true       -- 是否启用 C++20 标准库
}

vim.api.nvim_create_autocmd("BufNewFile", {
	pattern = { "*.c", "*.cc", "*.cpp", "*.cxx" },
	callback = function()
	-- 获取文件名（不带扩展名）
	local filename = vim.fn.expand("%:t:r")

	-- 动态判断语言类型
	local is_cpp = vim.fn.expand("%:e") == "cc"
	or vim.fn.expand("%:e") == "cpp"
	or vim.fn.expand("%:e") == "cxx"

	-- 构建模板内容
	local template = {
		"/*",
		" * Filename:    " .. vim.fn.expand("%:t"),
		" * Author:      " ..config.author,
		" * Email:       " ..config.email,
		" * Date:        " .. os.date("%Y-%m-%d %H:%M"),
		" * Environment: " ..config.environment,
		" * Tool:        " ..config.tool,
		" */",
		""
	}

	-- 添加语言特定内容
	if is_cpp then
		vim.list_extend(template, {
			"#include <iostream>",
			"",
			"using namespace std;",
			"",
			"int main(int argc, char** argv) {",
			"    ",
			"    return 0;",
			"}"
		})
		else  -- C 语言模板
			vim.list_extend(template, {
				"#include <stdio.h>",
				"#include <stdlib.h>",
				"",
				"int main(int argc, char** argv) {",
				"    ",
				"    return EXIT_SUCCESS;",
				"}"
			})
			end

			-- 插入模板并定位到可编辑位置
			vim.api.nvim_buf_set_lines(0, 0, -1, false, template)
			vim.api.nvim_win_set_cursor(0, {is_cpp and 11 or 10, 4}) -- 光标定位到 main 函数内部
			end
})

vim.api.nvim_create_autocmd("BufNewFile", {
	pattern = { "*.h", "*.hh", "*.hpp", "*.hxx" }, -- 支持多种扩展名
	callback = function(args)
	-- 获取文件名和扩展名（不含路径）
	local filename = vim.fn.expand("%:t:r")  -- 主文件名（无扩展名）
	local extension = vim.fn.expand("%:e")   -- 扩展名（如 h/hh/hpp）

	-- 处理特殊字符并标准化命名
	local guard_base = filename:gsub("[^%w]", "_"):upper()  -- 非字母数字转下划线
	local guard_ext = extension:upper()                     -- 扩展名大写
	local guard = "__"..guard_base.."_"..guard_ext.."_"     -- 拼接保护宏

	-- 插入保护宏（带智能注释）
	vim.api.nvim_buf_set_lines(0, 0, -1, false, {
		"/*",
		" * Filename:    " .. vim.fn.expand("%:t"),
		" * Author:      " .. config.author,
		" * Created:     " .. os.date("%Y-%m-%d"),
		" */",
		"#ifndef " .. guard,
		"#define " .. guard,
		"",
		"#endif /* " .. guard .. " */"
	})

	-- 光标定位到内容区（第3行）
	vim.api.nvim_win_set_cursor(0, {3, 0})
	end
})

-- 设置默认 shell (必须放在所有终端相关配置之前)
vim.o.shell = "/bin/zsh"
vim.o.shellcmdflag = "-ic"  -- 启用交互式 shell


vim.api.nvim_create_autocmd("TermOpen", {
	pattern = "*",
	callback = function()
	-- 处理带空格的路径
	local path = vim.fn.shellescape(vim.fn.expand("%:p:h"))

	-- 获取终端作业 ID
	local job_id = vim.b.terminal_job_id

	-- 检测当前 shell 类型
	local shell = vim.fn.system("echo $SHELL"):match("%w+$")

	-- 发送目录切换命令
	if shell == "zsh" then
		vim.defer_fn(function()
		-- 使用 zsh 的启动参数
		vim.api.nvim_chan_send(job_id, " cd "..path.." && exec zsh -i\n")
		end, 50) -- 延迟确保终端初始化完成
		end
		end
})
